package com.sqx.modules.health.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.RedisUtils;
import com.sqx.modules.health.entity.AppProduct;
import com.sqx.modules.health.mapper.AppProductMapper;
import com.sqx.modules.health.service.IAppProductService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gxy
 * @since 2022-08-12
 */
@Service
public class AppProductServiceImpl extends ServiceImpl<AppProductMapper, AppProduct> implements IAppProductService {

    private static final String PRODUCT_INFO_REDIS_KEY = "PRODUCT_INFO_";
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public AppProduct getRedisByUid(String uid) {
        String jsonString = redisUtils.get(PRODUCT_INFO_REDIS_KEY + uid);
        AppProduct info;
        if (StringUtil.isNullOrEmpty(jsonString)) {
            info = this.getById(uid);
            redisUtils.set(PRODUCT_INFO_REDIS_KEY + uid, JSONObject.toJSONString(info));
        } else {
            info = JSONObject.parseObject(jsonString, AppProduct.class);
        }
        return info;
    }
}
